package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.ModuleDao;
import com.itheima.domain.system.Module;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.ModuleService;
import com.itheima.utils.ArrayToListUtil;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;


public class ModuleServiceImpl implements ModuleService {
    @Override
    public PageInfo findAll(Integer page, Integer size) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
        PageHelper.startPage(page,size);
        List<Module> all = mapper.findAll();
        PageInfo<Module> modulePageInfo = new PageInfo<>(all);
        TransactionUtil.close(sqlSession);
        return modulePageInfo;
    }

    @Override
    public List<Module> findAll() {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
        List<Module> all = mapper.findAll();
        TransactionUtil.close(sqlSession);
        return all;
    }

    @Override
    public List<Map> findAuthorDataByRId(String roleId) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
        List<Map> authorDataByRoleId = mapper.findAuthorDataByRoleId(roleId);


        return authorDataByRoleId;
    }

    @Override
    public Module findById(String id) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
        Module module= mapper.findById(id);
        TransactionUtil.close(sqlSession);
        return module;
    }

    @Override
    public void delete(String...id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            mapper.delete(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void save(Module module) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            module.setId(UUID.randomUUID().toString());
            module.setState((long) 1);
            mapper.save(module);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void update(Module module) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            mapper.update(module);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public List<Module> findModuleByUserId(String userId) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao mapper = MapperFactory.getMapper(sqlSession, ModuleDao.class);
        List<Module> all = mapper.findModulleByUserId(userId);
        TransactionUtil.close(sqlSession);
        return all;
//        return findAll();
    }
}
